From 05bdddaa7b9e714e4cec5670e4fcbb1fd67c7072 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 21 Aug 2014 14:09:27 -0700 Subject: [PATCH] Add a --no-run option to `cargo test` This allows tests to be built, but not run. --- Makefile.in | 2 +- src/bin/cargo-bench.rs | 21 +++++++++++++-------- src/bin/cargo-test.rs | 20 ++++++++++++-------- src/cargo/ops/cargo_test.rs | 28 +++++++++++++++++----------- src/cargo/ops/mod.rs | 2 +- tests/test_cargo_test.rs | 22 ++++++++++++++++++++++ 6 files changed, 66 insertions(+), 29 deletions(-) diff --git a/Makefile.in b/Makefile.in index c9de8ceab..739ca9083 100644 --- a/Makefile.in +++ b/Makefile.in @@ -85,7 +85,7 @@ style: sh tests/check-style.sh no-exes: - find $$(git ls-files) -perm +111 -type f \ + find $$(git ls-files | grep -v ttf) -perm +111 -type f \ -not -name configure -not -name '*.sh' -not -name '*.rs' | \ grep '.*' \ && exit 1 || exit 0 diff --git a/src/bin/cargo-bench.rs b/src/bin/cargo-bench.rs index e700c13e8..54ca63227 100644 --- a/src/bin/cargo-bench.rs +++ b/src/bin/cargo-bench.rs @@ -21,7 +21,9 @@ Usage: Options: -h, --help Print this message + --no-run Compile, but don't run benchmarks -j N, --jobs N The number of jobs to run in parallel + --target TRIPLE Build for the target triple --manifest-path PATH Path to the manifest to build benchmarks for -v, --verbose Use verbose output @@ -39,16 +41,19 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path)); shell.set_verbose(options.flag_verbose); - let mut compile_opts = ops::CompileOptions { - update: false, - env: "bench", - shell: shell, - jobs: options.flag_jobs, - target: None, - dev_deps: true, + let mut ops = ops::TestOptions { + no_run: options.flag_no_run, + compile_opts: ops::CompileOptions { + update: false, + env: "bench", + shell: shell, + jobs: options.flag_jobs, + target: options.flag_target.as_ref().map(|s| s.as_slice()), + dev_deps: true, + }, }; - let err = try!(ops::run_benches(&root, &mut compile_opts, + let err = try!(ops::run_benches(&root, &mut ops, options.arg_args.as_slice()).map_err(|err| { CliError::from_boxed(err, 101) })); diff --git a/src/bin/cargo-test.rs b/src/bin/cargo-test.rs index 873c20eb0..40c495246 100644 --- a/src/bin/cargo-test.rs +++ b/src/bin/cargo-test.rs @@ -21,6 +21,7 @@ Usage: Options: -h, --help Print this message + --no-run Compile, but don't run tests -j N, --jobs N The number of jobs to run in parallel --target TRIPLE Build for the target triple -u, --update-remotes Deprecated option, use `cargo update` instead @@ -40,16 +41,19 @@ fn execute(options: Options, shell: &mut MultiShell) -> CliResult> { let root = try!(find_root_manifest_for_cwd(options.flag_manifest_path)); shell.set_verbose(options.flag_verbose); - let mut compile_opts = ops::CompileOptions { - update: options.flag_update_remotes, - env: "test", - shell: shell, - jobs: options.flag_jobs, - target: options.flag_target.as_ref().map(|s| s.as_slice()), - dev_deps: true, + let mut ops = ops::TestOptions { + no_run: options.flag_no_run, + compile_opts: ops::CompileOptions { + update: options.flag_update_remotes, + env: "test", + shell: shell, + jobs: options.flag_jobs, + target: options.flag_target.as_ref().map(|s| s.as_slice()), + dev_deps: true, + }, }; - let err = try!(ops::run_tests(&root, &mut compile_opts, + let err = try!(ops::run_tests(&root, &mut ops, options.arg_args.as_slice()).map_err(|err| { CliError::from_boxed(err, 101) })); diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index d1c0ad13e..7e8995769 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -5,14 +5,20 @@ use sources::PathSource; use ops; use util::{CargoResult, ProcessError}; +pub struct TestOptions<'a> { + pub compile_opts: ops::CompileOptions<'a>, + pub no_run: bool, +} + pub fn run_tests(manifest_path: &Path, - options: &mut ops::CompileOptions, - args: &[String]) -> CargoResult> { + options: &mut TestOptions, + test_args: &[String]) -> CargoResult> { let mut source = try!(PathSource::for_path(&manifest_path.dir_path())); try!(source.update()); let package = try!(source.get_root_package()); - let mut compile = try!(ops::compile(manifest_path, options)); + let mut compile = try!(ops::compile(manifest_path, &mut options.compile_opts)); + if options.no_run { return Ok(None) } compile.tests.sort(); let cwd = os::getcwd(); @@ -21,11 +27,11 @@ pub fn run_tests(manifest_path: &Path, Some(path) => path, None => exe.clone(), }; - let cmd = compile.process(exe, &package).args(args); - try!(options.shell.concise(|shell| { + let cmd = compile.process(exe, &package).args(test_args); + try!(options.compile_opts.shell.concise(|shell| { shell.status("Running", to_display.display().to_string()) })); - try!(options.shell.verbose(|shell| { + try!(options.compile_opts.shell.verbose(|shell| { shell.status("Running", cmd.to_string()) })); match cmd.exec() { @@ -42,7 +48,7 @@ pub fn run_tests(manifest_path: &Path, }); for (lib, name) in libs { - try!(options.shell.status("Doc-tests", name)); + try!(options.compile_opts.shell.status("Doc-tests", name)); let mut p = compile.process("rustdoc", &package) .arg("--test").arg(lib) .arg("--crate-name").arg(name) @@ -51,8 +57,8 @@ pub fn run_tests(manifest_path: &Path, .cwd(package.get_root()); // FIXME(rust-lang/rust#16272): this should just always be passed. - if args.len() > 0 { - p = p.arg("--test-args").arg(args.connect(" ")); + if test_args.len() > 0 { + p = p.arg("--test-args").arg(test_args.connect(" ")); } for (pkg, libs) in compile.libraries.iter() { @@ -64,7 +70,7 @@ pub fn run_tests(manifest_path: &Path, } } - try!(options.shell.verbose(|shell| { + try!(options.compile_opts.shell.verbose(|shell| { shell.status("Running", p.to_string()) })); match p.exec() { @@ -77,7 +83,7 @@ pub fn run_tests(manifest_path: &Path, } pub fn run_benches(manifest_path: &Path, - options: &mut ops::CompileOptions, + options: &mut TestOptions, args: &[String]) -> CargoResult> { let mut args = args.to_vec(); args.push("--bench".to_string()); diff --git a/src/cargo/ops/mod.rs b/src/cargo/ops/mod.rs index 16eb105e9..71846ecf6 100644 --- a/src/cargo/ops/mod.rs +++ b/src/cargo/ops/mod.rs @@ -7,7 +7,7 @@ pub use self::cargo_new::{new, NewOptions}; pub use self::cargo_doc::{doc, DocOptions}; pub use self::cargo_generate_lockfile::{generate_lockfile, write_resolve}; pub use self::cargo_generate_lockfile::{update_lockfile, load_lockfile}; -pub use self::cargo_test::{run_tests, run_benches}; +pub use self::cargo_test::{run_tests, run_benches, TestOptions}; mod cargo_clean; mod cargo_compile; diff --git a/tests/test_cargo_test.rs b/tests/test_cargo_test.rs index 38980e77f..2b4ed8a47 100644 --- a/tests/test_cargo_test.rs +++ b/tests/test_cargo_test.rs @@ -852,3 +852,25 @@ test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured fresh = FRESH, dir = p.url()).as_slice())); }) + +test!(test_no_run { + let p = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + "#) + .file("src/lib.rs", " + #[test] + fn foo() { fail!() } + "); + + assert_that(p.cargo_process("cargo-test").arg("--no-run"), + execs().with_status(0) + .with_stdout(format!("\ +{compiling} foo v0.0.1 ({dir}) +", + compiling = COMPILING, + dir = p.url()).as_slice())); +}) -- 2.30.2